<?php
/**
 * PsiFramework
 *
 * Php Simple Framework
 *
 * PHP version 5.3
 *
 * @category  PsiFramework.Plugins.Recaptcha.Classes.Tools
 * @package   PsiFramework.Plugins.Recaptcha.Classes.Tools
 * @author    Remi San <remi.san@gmail.com>
 * @copyright 2011 Remi San
 * @license   http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @version   SVN: <svn_id>
 * @link      http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\ReCaptcha\Classes\Tools\Captcha.html
 */

namespace PsiFramework\Plugins\Recaptcha\Classes\Tools ;

importLibrary("Plugins/Recaptcha/Lib/recaptchalib.php");
import("Classes.Util.HTTP.Request");
import("Classes.Util.Manager.ParametersManager");

use \PsiFramework\Classes\Util\HTTP\Request;
use \PsiFramework\Classes\Util\Manager\ParametersManager;
use \ReCaptchaResponse;

/**
 * The Captcha class
 *
 * This is the class to manage the recaptcha library
 *
 * @category PsiFramework.Plugins.Recaptcha.Classes.Tools
 * @package  PsiFramework.Plugins.Recaptcha.Classes.Tools
 * @author   Remi San <remi.san@gmail.com>
 * @license  http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @link     http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\ReCaptcha\Classes\Tools\Captcha.html
 */
class Captcha
{
    /**
     * The public key
     * 
     * @var string
     */
    protected /*string*/            $_publicKey ;
    /**
     * The private key
     * 
     * @var string
     */
    protected /*string*/            $_privateKey ;
    /**
     * The HTTP request
     * 
     * @var string
     */
    protected /*Request*/           $_request ;
    /**
     * The captcha response
     * 
     * @var ReCaptchaResponse
     */
    protected /*ReCaptchaResponse*/ $_response ;

    /**
     * Singleton
     * 
     * @var Captcha
     */
    protected static /*Captcha*/    $_singleton ;

    /**
     * Constructor
     * 
     * @return Captcha
     */
    protected function __construct()
    {
        $this->_request = Request::getInstance();
        $this->_response = null ;

        $paramManager = ParametersManager::getInstance();
        $applicationsParameters = $paramManager->getApplicationsParameters();

        $this->_publicKey = $applicationsParameters->getCaptchaPublicKey();
        $this->_privateKey = $applicationsParameters->getCaptchaPrivateKey();
    }

    /**
     * Show the captcha code form on screen
     *
     * @param string $lang the language
     * 
     * @return void
     */
    public function showCaptchaCode(/*string*/ $lang="en")
    {
        $options = "<script>".
                        "var RecaptchaOptions = {".
                            "theme : 'white', lang : '".$lang."'".
                        "};".
                   "</script>" ;
        echo $options.recaptcha_get_html($this->_publicKey);
    }

    /**
     * Retrieves the response (true if the code is ok, else false)
     *
     * @return bool
     */
    public function retrieveResponse()
    {
        $address = $this->_request->getServerParameter("REMOTE_ADDR");
        $challenge
            = $this->_request->getPostParameter("recaptcha_challenge_field");
        $response
            = $this->_request->getPostParameter("recaptcha_response_field");
        $this->_response
            = recaptcha_check_answer(
                $this->_privateKey,
                $address,
                $challenge,
                $response
            );
        return $this->_response->is_valid ;
    }

    /**
     * Returns the error
     *
     * @return string
     */
    public function getError()
    {
        return $this->_response->error ;
    }

    /**
     * This function must be called instead of the construtor
     *
     * @return Captcha
     */
    public static function getInstance()
    {

        if (self::$_singleton == null) {
            self::$_singleton = new Captcha();
        }

        return self::$_singleton;
    }
}

